package com.company.oa.hr.service.employee.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.common.exception.BusinessException;
import com.company.oa.hr.dto.employee.ContractDTO;
import com.company.oa.hr.entity.employee.Contract;
import com.company.oa.hr.mapper.employee.ContractMapper;
import com.company.oa.hr.service.employee.ContractService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Override
    public IPage<Contract> getContractPage(int page, int size, Long employeeId) {
        Page<Contract> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Contract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(employeeId != null, Contract::getEmployeeId, employeeId)
                   .orderByDesc(Contract::getCreateTime);
        return page(pageParam, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createContract(ContractDTO contractDTO, Long creatorId) {
        // 检查合同编号是否已存在
        LambdaQueryWrapper<Contract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Contract::getContractNo, contractDTO.getContractNo());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("合同编号已存在");
        }
        
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractDTO, contract);
        
        // 设置合同状态为生效中
        contract.setStatus(1);
        
        // 设置创建信息
        contract.setCreatorId(creatorId);
        contract.setCreateTime(new Date());
        
        return save(contract);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateContract(ContractDTO contractDTO) {
        // 检查合同是否存在
        Contract existContract = getById(contractDTO.getId());
        if (existContract == null) {
            throw new BusinessException("合同不存在");
        }
        
        // 检查合同编号是否已被其他合同使用
        LambdaQueryWrapper<Contract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Contract::getContractNo, contractDTO.getContractNo())
                   .ne(Contract::getId, contractDTO.getId());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("合同编号已被其他合同使用");
        }
        
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractDTO, contract);
        
        // 设置更新时间
        contract.setUpdateTime(new Date());
        
        return updateById(contract);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteContract(Long id) {
        // 检查合同是否存在
        Contract existContract = getById(id);
        if (existContract == null) {
            throw new BusinessException("合同不存在");
        }
        
        return removeById(id);
    }

    @Override
    public String uploadContractAttachment(MultipartFile file) {
        // 实际项目中需要实现文件上传逻辑
        // 这里简单返回一个模拟的文件路径
        return "/uploads/contracts/" + UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
    }

    @Override
    public List<Contract> getEmployeeContracts(Long employeeId) {
        LambdaQueryWrapper<Contract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Contract::getEmployeeId, employeeId)
                   .orderByDesc(Contract::getCreateTime);
        return list(queryWrapper);
    }
} 